import {defineConfig, loadEnv} from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import {resolve} from 'path'
import {createSvgIconsPlugin} from 'vite-plugin-svg-icons'
// https://vitejs.dev/config/
export default defineConfig(({command, mode}) => {
    let env = loadEnv(mode, process.cwd())
    return {
        base:'/',
        plugins: [
            vue(),
            createSvgIconsPlugin({
                // Specify the icon folder to be cached
                iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
                // Specify symbolId format
                symbolId: 'icon-[dir]-[name]',
            }),
        ],
        resolve: {
            alias: [ // 配置 @ 指代 src
                {
                    find: "@",
                    replacement: resolve(__dirname, "./src"),
                },
            ],
        },
        css: {
            preprocessorOptions: {
                scss: {
                    javascriptEnabled: true,
                    additionalData: '@import "./src/styles/variable.scss";'
                }
            }
        },
        build:{
            rollupOptions: {
                output: {
                    chunkFileNames: 'assets/js/[name]-[hash].js',
                    entryFileNames: 'assets/js/[name]-[hash].js',
                    assetFileNames: (assetInfo) => {
                        if (assetInfo.name) {
                            if (/\.(png|jpg|jpeg|gif)$/gi.test(assetInfo.name)) {
                                return 'assets/img/[name]-[hash][extname]'
                            } else if (/\.(ttf|woff|woff2)$/gi.test(assetInfo.name)) {
                                return 'assets/font/[name]-[hash][extname]'
                            }
                        }
                        return 'assets/[ext]/[name]-[hash][extname]'
                    }
                }
            }
        },
        server: {
            port:5174,
            proxy: {
                "/newApi": {
                    // target: 'http://19.120.74.72:9527',
                    target: "http://19.120.74.40:9999",
                    changeOrigin: true, // 允许websockets跨域
                    // ws: true,
                    rewrite: (path) => path.replace('newApi', ''),
                },
                "/localNode": {
                    // target: 'http://19.120.74.72:9527',
                    target: "http://127.0.0.1:3000",
                    changeOrigin: true, // 允许websockets跨域
                    // ws: true,
                    rewrite: (path) => path.replace('localNode', ''),
                },
                "/reportApi": {
                    // target: 'http://19.120.74.72:9527',
                    target: "http://19.120.74.92:8080/",
                    changeOrigin: true, // 允许websockets跨域
                    // ws: true,
                    rewrite: (path) => path.replace('reportApi', ''),
                },
            },
        }
    }
})
